/**
 * Return the moth label (3 character) from the month number
 */
function getMonthName(month) {
	var monthName = "";
	switch (month) {
		case 1:
			monthName = "Jan";
			break;
		case 2:
			monthName = "Feb";
			break;
		case 3:
			monthName = "Mar";
			break;
		case 4:
			monthName = "Apr";
			break;
		case 5:
			monthName = "May";
			break;
		case 6:
			monthName = "Jun";
			break;
		case 7:
			monthName = "Jul";
			break;
		case 8:
			monthName = "Aug";
			break;
		case 9:
			monthName = "Sep";
			break;
		case 10:
			monthName = "Oct";
			break;
		case 11:
			monthName = "Nov";
			break;
		case 12:
			monthName = "Dec";
			break;
	}
	return monthName;
}

/**
 * Return the month number from the month name (3 characters)
 */
function getMonthNumber(month) {
	var m = "";
	switch (month) {
		case "Jan":
			m = "01";
			break;
		case "Feb":
			m = "02"
			break;
		case "Mar":
			m = "03";
			break;
		case "Apr":
			m = "04";
			break;
		case "May":
			m = "05";
			break;
		case "Jun":
			m = "06";
			break;
		case "Jul":
			m = "07";
			break;
		case "Aug":
			m = "08";
			break;
		case "Sep":
			m = "09";
			break;
		case "Oct":
			m = "10";
			break;
		case "Nov":
			m = "11";
			break;
		case "Dec":
			m = "12";
			break;
	}
	return m;
}

/**
 * Return an array of data labels "month year" from starting and ending date
 */
function createlabels(startX, endX, datatime) {
	startX = startX + "";
	endX = endX + ""
	var labels = new Array();
	if(datatime == "month") {
		var startYear = parseInt(startX.split("-")[0], 10)
		var startMonth = parseInt(startX.split("-")[1], 10)
		var endYear = parseInt(endX.split("-")[0], 10)
		var endMonth = parseInt(endX.split("-")[1], 10)
		while(startYear < endYear || (startYear == endYear && startMonth <= endMonth)) {
			var date = getMonthName(startMonth) + " " + startYear;
			labels.push(date);
			startMonth += 1;
			if(startMonth == 13) {
				startMonth = 1;
				startYear += 1;
			}
		}
	} else if(datatime == "year") {
		var startYear = parseInt(startX.split("-")[0], 10)
		var endYear = parseInt(endX.split("-")[0], 10)
		while(startYear <= endYear) {
			var date = startYear;
			labels.push(date);
			startYear += 1;
		}
	} else if(datatime == "day") {
		var startYear = parseInt(startX.split("-")[0], 10)
		var startMonth = parseInt(startX.split("-")[1], 10)
		var startDay = parseInt(startX.split("-")[2], 10)
		var endYear = parseInt(endX.split("-")[0], 10)
		var endMonth = parseInt(endX.split("-")[1], 10)
		var endDay = parseInt(endX.split("-")[2], 10)
		while(startYear < endYear || (startYear == endYear && startMonth < endMonth) || (startYear == endYear && startMonth == endMonth && startDay <= endDay)) {
			//alert(startYear +"-"+ endYear +" / "+ startMonth +"-"+ endMonth +" / "+ startDay +"-"+ endDay)
			var date = startDay + " " + getMonthName(startMonth) + " " + startYear;
			labels.push(date);
			startDay += 1;
			if(startDay == 29 && startMonth == 2) {
				startDay = 1;
				startMonth += 1;
			} else if(startDay == 31 && (startMonth == 4 || startMonth == 6 || startMonth == 9 || startMonth == 11)) {
				startDay = 1;
				startMonth += 1;
			} else if(startDay == 32) {
				startDay = 1;
				startMonth += 1;
			}
			if(startMonth == 13) {
				startMonth = 1;
				startYear += 1;
			}
		}
	}

	return labels;
}

/**
 * Function to create a time representation "YYYY-MM" from a label "Mmm YYYY"
 */
function createYearMonthFromLabel(label) {
	var y = label.split(" ")[1]
	var m = label.split(" ")[0]
	return y + "-" + getMonthNumber(m);
}

/**
 * Function to create a time representation "YYYY-MM-DD" from a label "dd Mmm YYYY"
 */
function createYearMonthDayFromLabel(label) {
	var y = label.split(" ")[2]
	var m = label.split(" ")[1]
	var d = label.split(" ")[0]
	return y + "-" + getMonthNumber(m) + "-" + d;
}